package service;

import java.math.BigDecimal;

import model.ClassPojo;
import model.ObjectPojo;
import model.PropPojo;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

import dao.ClassDao;
import dao.ClassDefDao;
import dao.ObjectDao;
import dao.PropDao;
import dao.TripleDao;

public class SaveItems {

	public void saveClass(OntClass cla){
		
		ClassPojo cpojo = new ClassPojo(cla.getLocalName(), cla.getURI(),  cla.getNameSpace());
		ClassDao cdao = new ClassDao();
		cdao.insertClassPojo(cpojo);
		
	}

	public void saveSubClass(OntClass cla, OntClass subCla){
		
		//saves a pair of class and subclass
		ClassPojo claPojo = new ClassPojo(cla.getLocalName(), cla.getURI(), cla.getNameSpace());
		ClassPojo subClaPojo = new ClassPojo(subCla.getLocalName(), subCla.getURI(), subCla.getNameSpace());
		
		ClassDao cdao = new ClassDao();
		cdao.insertSubClass(claPojo, subClaPojo);
		
	}
	
	public void saveClassDefn(OntResource cla, OntProperty prop){
		
		//saves a pair of a class and a property
		ClassPojo cpojo = new ClassPojo(cla.getLocalName(), cla.getURI(), cla.getURI());
		PropPojo ppojo = new PropPojo(prop.getURI(),prop.getLocalName(),prop.getLocalName());
		
		ClassDefDao cddao = new ClassDefDao();
		cddao.insertClassDouble(cpojo,ppojo);
		
	}
	
	public void saveProperty(OntProperty pro){
		
		PropPojo prop = new PropPojo(pro.getURI(), pro.getLocalName(), pro.getNameSpace());
		PropDao pdao = new PropDao();
		pdao.insertPropPojo(prop);
		
	}
	
	public void saveObject(Resource object){
	
		ObjectPojo objPojo = new ObjectPojo(object.getURI(), object.getLocalName(), object.getNameSpace());
		ObjectDao odao = new ObjectDao();
		odao.insertObjectPojo(objPojo);
		
	}
	
	public void saveObjectDefn(Resource obj, OntClass cla){
		
		//registers the class to which an object belongs
		
	}
	
	public void saveTriple(Resource sub, Property pro, RDFNode obj){
		
		
		ObjectPojo subPojo = new ObjectPojo(sub.getLocalName(), sub.getURI(), sub.getNameSpace());
		PropPojo propPojo = new PropPojo(pro.getURI(), pro.getLocalName(),pro.getNameSpace());
		TripleDao tdao = new TripleDao();
		if(obj.isResource()){
			// handle in a simple way
			System.out.println("SUB: " +sub+" PRO: "+pro+" OBJ: "+obj);
			Resource objAsR = obj.asResource();
			
			ObjectPojo objPojo = new ObjectPojo(objAsR.getLocalName(), objAsR.getURI(), objAsR.getNameSpace());

			
			tdao.insertTripleDao(subPojo, propPojo, objPojo);
		}
		
		if(obj.isLiteral()){
			// do some other stuff
			
			String objAsString = obj.asLiteral().toString();
			
			try{
			BigDecimal objAsNum = new BigDecimal(objAsString);
			System.out.println("LITERAL: SUB: " +sub+" PRO: "+pro+" OBJ: "+objAsNum);
			tdao.insertTripleDao(subPojo, propPojo, objAsNum);
			} catch (NumberFormatException e) {
				System.out.println("LITERAL: SUB: " +sub+" PRO: "+pro+" OBJ: "+objAsString);
				tdao.insertTripleDao(subPojo, propPojo, objAsString);
			} 
			

		}
	
	}
}